package com.bxd.baodan.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.CompanySortBean;
import com.bxd.baodan.item.CompanySortBean.CompanyBean;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.GsonUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.QuickIndexBar;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

import java.util.ArrayList;
import java.util.List;

public class OneStepLiPeiActivity extends BaseActivity {
    private QuickIndexBar quickIndexBar;
    private ListView listview;
    private TextView currentWord;
    private List<CompanyBean> list = new ArrayList<>();

    private CommonAdapter<CompanyBean> companyAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_step_li_pei);
        setActionBarTitle("一键理赔");
        quickIndexBar = (QuickIndexBar) findViewById(R.id.quickIndexBar);
        listview = (ListView) findViewById(R.id.listview);
        currentWord = (TextView) findViewById(R.id.currentWord);
        setAdapter();
        listview.setAdapter(companyAdapter);
        setLetterListener();
        getData();

    }

    private void setAdapter() {
        companyAdapter = new CommonAdapter<CompanyBean>(this, list, R.layout.company_sort_item) {
            @Override
            public void convert(ViewHolder holder, final CompanyBean item) {
                TextView firstWord = holder.getView(R.id.first_word);
                TextView name = holder.getView(R.id.name);
                TextView phone = holder.getView(R.id.phone);
                ImageView iv = holder.getView(R.id.iv);
                View main = holder.getView(R.id.main);

                main.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        call(item.company_telephone);

                    }
                });

                GlideUtil.loadCircle(OneStepLiPeiActivity.this, item.company_image, iv);
                phone.setText(item.company_telephone);
                name.setText(item.company_short_name);
                String currentWord = item.sort;

                if (holder.getPosition() > 0) {
                    String lastWord = list.get(holder.getPosition() - 1).sort;
                    //拿当前的首字母和上一个首字母比较
                    if (currentWord.equals(lastWord)) {
                        //说明首字母相同，需要隐藏当前item的first_word
                        firstWord.setVisibility(View.GONE);
                    } else {
                        //不一样，需要显示当前的首字母
                        //由于布局是复用的，所以在需要显示的时候，再次将first_word设置为可见
                        firstWord.setVisibility(View.VISIBLE);
                        if (item.sort.equals("HOT")) {
                            firstWord.setText("热门公司");
                        } else {
                            firstWord.setText(item.sort);
                        }
                    }
                } else {
                    firstWord.setVisibility(View.VISIBLE);
                    if (item.sort.equals("HOT")) {
                        firstWord.setText("热门公司");
                    } else {
                        firstWord.setText(item.sort);
                    }
                }

            }
        };

    }

    private void getData() {
        NetUtil netUtil = new NetUtil(this, JsonApi.CLAIM_COMPANYS);

        netUtil.postRequest("...", new NetUtil.DataListener() {
            @Override
            public void onComplete(String data) {
                String str = JSONUtil.getData("list", data);
                List<CompanySortBean> netList = GsonUtil.fromJsonList(str, CompanySortBean.class);
                list.clear();
                for (CompanySortBean bean :
                        netList) {
                    for (CompanyBean companyBean :
                            bean.company) {
                        list.add(companyBean);
                    }

                }
                companyAdapter.notifyDataSetChanged();


            }
        });
    }

    private void setLetterListener() {


        quickIndexBar.setOnTouchLetterListener(new QuickIndexBar.OnTouchLetterListener() {
            @Override
            public void onTouchLetter(String letter) {
                //根据当前触摸的字母，去集合中找那个item的首字母和letter一样，然后将对应的item放到屏幕顶端
                for (int i = 0; i < list.size(); i++) {
                    String firstWord = list.get(i).sort;
                    if (letter.equals(firstWord)) {
                        //说明找到了，那么应该讲当前的item放到屏幕顶端
                        listview.setSelection(i);
                        break;//fixme 只需要找到第一个就行,不然会跳到下一个字母之上
                    } else if (letter.equals("热") && firstWord.equals("HOT")) {
                        listview.setSelection(i);
                        break;
                    }

            }

            //显示当前触摸的字母
            showCurrentWord(letter);
        }
    });


    //通过缩小currentWord来隐藏
        ViewHelper.setScaleX(currentWord,0);
        ViewHelper.setScaleY(currentWord,0);

    //		Log.e("tag", PinYinUtil.getPinyin("黑    马"));//HEIMA
    //		Log.e("tag", PinYinUtil.getPinyin("#黑**马"));//#HEI**MA
    //		Log.e("tag", PinYinUtil.getPinyin("O(∩_∩)O~黑。，马"));//HEIMA
}

    private boolean isScale = false;
    private Handler handler = new Handler();

    protected void showCurrentWord(String letter) {
        currentWord.setText(letter);
        //todo isScale  是否开启了缩放动画,没有缩放时才开启缩放
        if (!isScale) {
            isScale = true;

            ViewPropertyAnimator.animate(currentWord).scaleX(1f)
                    .setInterpolator(new OvershootInterpolator())
                    .setDuration(450).start();
            ViewPropertyAnimator.animate(currentWord).scaleY(1f)
                    .setInterpolator(new OvershootInterpolator())
                    .setDuration(450).start();
        }

        //fixme 先移除之前的任务 不然的话会有闪烁现象
        handler.removeCallbacksAndMessages(null);

        //延时隐藏currentWord
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //				currentWord.setVisibility(View.INVISIBLE);
                ViewPropertyAnimator.animate(currentWord).scaleX(0f).setDuration(450).start();
                ViewPropertyAnimator.animate(currentWord).scaleY(0f).setDuration(450).start();
                isScale = false;
            }
        }, 1500);
    }

    /**
     * 调用拨号界面
     * @param phone 电话号码
     */
    private void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

}
